package com.example.voucher.config;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import lombok.Data;

@Component
@ConfigurationProperties(prefix = com.example.voucher.common.VoucherConstant.VOUCHER_CHANNEL_CONFIG_PROPERTIES_PREFIX)
@Data
public class VoucherChannelConfig {

    private List<String> easTenantList;
    private List<String> ncTenantList;

    @Autowired
    private TenantMirrorConfig mirrorConfig;

    public boolean isEasChannel(String tenantCode) {
        if (CollectionUtils.isNotEmpty(getEasTenantList()) && easTenantList.contains(tenantCode)) {
            return true;
        }
        if (CollectionUtils.isNotEmpty(getNcTenantList()) && ncTenantList.contains(tenantCode)) {
            return false;
        }
        String mirrorTenantCode = mirrorConfig.getMirror(tenantCode);
        if (StringUtils.isNotBlank(mirrorTenantCode)) {
            return easTenantList.contains(mirrorTenantCode);
        }
        return false;
    }

    public boolean isNcChannel(String tenantCode) {
        if (CollectionUtils.isNotEmpty(getEasTenantList()) && easTenantList.contains(tenantCode)) {
            return false;
        }
        if (CollectionUtils.isNotEmpty(getNcTenantList()) && ncTenantList.contains(tenantCode)) {
            return true;
        }
        String mirrorTenantCode = mirrorConfig.getMirror(tenantCode);
        if (StringUtils.isNotBlank(mirrorTenantCode)) {
            return ncTenantList.contains(mirrorTenantCode);
        }
        return false;
    }

}
